package vn.com.pavibook.admin.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import vn.com.pavibook.dao.MoneyDao;
import vn.com.pavibook.domain.model.Money;
import vn.com.pavibook.domain.model.PagingInfo;
import vn.com.pavibook.domain.model.SearchResult;
import vn.com.pavibook.model.vo.MoneyVO;

@Service
public class ManageMoneyService {
	
	@Autowired
	private MoneyDao moneyDao;
	
	public Model viewListMoney(Model model, PagingInfo pagingInfo, String keyword){

		SearchResult<Money> result = moneyDao.getMoney(pagingInfo, keyword);
		List<Money> moneyList = result.getSearchResult();
		PagingInfo pagingInfoNew = result.getPagingInfo();
		model.addAttribute("pagingInfo", pagingInfoNew);
		model.addAttribute("moneyList", moneyList);
		model.addAttribute("keyword", keyword);
		return model;
	}
	
	public void deleteMoney(int id){
		moneyDao.deleteMoney(id);
	}
	
	public void saveMoney(MoneyVO moneyVO, boolean isEdit){
		if(!isEdit){
			moneyDao.saveMoney(moneyVO);
		}else{
			moneyDao.editMoney(moneyVO);
		}
	}
	
	public boolean checkExistCurrency(String newCurrency){
		return moneyDao.checkExistCurrency(newCurrency);
	}
	
	public MoneyVO getMoneyById(int id){
		Money money = moneyDao.getMoneyById(id);
		MoneyVO moneyVO = new MoneyVO();
		if(money != null){
			moneyVO.setId(money.getId());
			moneyVO.setName(money.getName());
			moneyVO.setCurrency(money.getCurrency());
			moneyVO.setRate(money.getRate());
			moneyVO.setActive(money.isActive());
		}
		return moneyVO;
	}
}
